package opsControllers

import (
	"fmt"
	"lazyops_go/models"
	"lazyops_go/tasks"
	"lazyops_go/tools"
	"os"
	"path"
)

func (c *OpsController) IndexScript() {
	cur_page, _ := c.GetInt(":page", 1)
	cur_user_menus := c.GetSession("cur_user_menus")
	cur_user := c.GetSession("cur_user")
	label_obj := models.QueryLabelAll()
	var ztree_data_list []models.ZtreeDate
	for _, v := range label_obj {
		label_id := v.ID
		host_obj := models.QueryAssetSearchLabel(label_id)
		var ztr_head models.ZtreeDate
		ztr_head.Id = label_id
		ztr_head.Pid = 0
		ztr_head.Name = v.LabelName
		ztr_head.Open = false
		ztree_data_list = append(ztree_data_list, ztr_head)
		for _, vv := range host_obj {
			var ztree models.ZtreeDate
			ztree.Id = vv.ID*1000
			ztree.Pid = label_id
			ztree.Name = fmt.Sprintf("%s_%s", vv.Ip, vv.HostName)
			ztree.Open = false
			ztree_data_list = append(ztree_data_list, ztree)
		}
	}
	data_list := models.PageScript(cur_page)
	page := tools.CreatePaging(cur_page, 12, cap(data_list))
	c.Data["page"] = page
	c.Data["data_list"] = data_list
	c.Data["ztree_data"] = ztree_data_list
	c.Data["cur_user"] = cur_user
	c.Data["cur_user_menus"] = cur_user_menus
	c.Data["label"] = "脚本管理"
	c.XSRFExpire = 7200
	c.Data["xsrf_token"] = c.XSRFToken()
	c.Layout = "base/base.html"
	c.LayoutSections = make(map[string]string)
	c.LayoutSections["Scripts"] = "ops/scriptjs.html"
	c.LayoutSections["HtmlHead"] = "ops/scriptcss.html"
	c.TplName = "ops/script.html"
}

func (c *OpsController) CreateScript() {
	script_name := c.GetString("script_name")
	script_msg := c.GetString("script_msg")
	script_arg := c.GetString("script_arg")
	script_text := c.GetString("script_text")
	cur_user := c.GetSession("cur_user").(string)
	obj := models.OpsScript{}
	obj.ScriptName = script_name
	obj.ScriptArg = script_arg
	obj.ScriptMsg = script_msg
	obj.ScriptText = script_text
	obj.AddUser = cur_user
	models.AddScript(obj)
	c.Data["json"] = "添加成功"
	c.ServeJSON()
}

func (c *OpsController) GetScript() {
	id, _ := c.GetInt("edit_id", 0)
	obj := models.GetScriptId(id)
	c.Data["json"] = obj
	c.ServeJSON()
}

func (c *OpsController) EditScript() {
	id, _ := c.GetInt("edit_id", 0)
	script_name := c.GetString("script_name")
	script_msg := c.GetString("script_msg")
	script_arg := c.GetString("script_arg")
	script_text := c.GetString("script_text")
	cur_user := c.GetSession("cur_user").(string)
	obj := models.OpsScript{}
	obj.ScriptName = script_name
	obj.ScriptArg = script_arg
	obj.ScriptMsg = script_msg
	obj.ScriptText = script_text
	obj.AddUser = cur_user
	models.UpScript(id, obj)
	c.Data["json"] = "修改成功"
	c.ServeJSON()
}

func (c *OpsController) DelScript() {
	id, _ := c.GetInt("del_id", 0)
	models.DelScript(id)
	c.Data["json"] = "已删除"
	c.ServeJSON()
}

func (c *OpsController) DeployScript() {
	ip_list := c.GetString("ip_list")
	exec_id, _ := c.GetInt("exec_id", 0)
	obj := models.GetScriptId(exec_id)
	dir, _ := os.Getwd()
	save_to_file := path.Join("static/upload/scripts/",obj.ScriptName)

	//salt执行脚本路径
	salt_script := path.Join(dir,save_to_file)

	f,err := os.Create(save_to_file)
	defer f.Close()
	if err !=nil {
		fmt.Println(err.Error())
	} else {
		_,err=f.Write([]byte(obj.ScriptText))
	}

	// 异步执行
	taskName := "task.DeployScript"
	cli := tasks.Client()
	asyncResult, err := cli.Delay(taskName, ip_list,salt_script,obj.ScriptArg)
	if err != nil {
		panic(err)
	}
	//添加任务记录
	tk := models.LogTask{}
	cur_user := c.GetSession("cur_user")
	tk.UserName = cur_user.(string)
	tk.TaskName = fmt.Sprintf("脚本部署：%s---%s",obj.ScriptName,ip_list)
	tk.Status = "PENDING"
	tk.TaskId = asyncResult.TaskID
	models.AddTask(tk)
	c.Data["json"] = "部署中:"+asyncResult.TaskID
	c.ServeJSON()
}
